package socket;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class ServerDemo {
	public static void main(String[] args) {
		try {
			@SuppressWarnings("resource")
			ServerSocket server = new ServerSocket(8899);
			System.out.println("服务器启动");
			System.out.println(server.getLocalPort());
			// System.out.println(server.isBound());
			Socket socket = server.accept();
			System.out.println(socket.getPort());
			System.out.println("客户端连接");
			ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
			ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
			Scanner kbin=new Scanner(System.in);
			String m,send;
			do {
				m = (String)in.readObject();//从网络上接收信息
				System.out.println("客户端说:"+m);
				send = kbin.nextLine();//键盘输入
				out.writeObject(send);
				out.flush();
			} while (!m.equals("bye"));
			kbin.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			// e.printStackTrace();
			System.out.println("在同一个电脑上只能运行一个实例！");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
